How to "Continue" in a Process Block?
I can't seem to figure out a way to properly skip the rest of the Process Block without completely exiting it.  Take the following as an example:

function Test
{
	Begin
	{ $i = 1 }

	Process
	{
		if ($i -eq 10) { Write-Error 'Some Error' }
		Write-Host ($i++)
	}
}

cls
1..20 | Test

Write-Error does not terminate the Process block.  However, it doesn't skip the rest of the block as I intend.

Using any keywords such as break, continue, return, or throw will shut down the pipeline completely.

How exactly can I achieve something similar to the effect of "continue" in a loop?

December 19th, 2013 8:41pm

You know what, return actually does work, and is the only one keyword that will work.

It works like this:

function Test
{
	Begin
	{ $i = 0 }

	Process
	{
		$i++
		if ($i -eq 10) { Write-Error 'Some Error'; return }
		Write-Host $i
	}
}

cls
1..20 | Test


Free Windows Admin Tool Kit Click here and download it now
December 19th, 2013 9:04pm

Thanks AverageJoeOfToronto for taking the time to answer your own question.  Saved me a few minutes of having to muddle through this myself! :)
June 26th, 2015 4:48am

None of the above is correct even if it appears to work due to numerous syntax and logic errors.

This is how this works.

function Test
{
     Param(
       [Parameter(ValueFromPipeline)]
        $val
      )

	Process
	{
                Try{
		if ($val -eq 10) { Throw "Here is my error Error" }
		Write-Host ($val)
                }
                Catch{"$_"}
	}
}

PS C:\scripts>1..20 | Test
1
2
3
4
5
6
7
8
9
Here is my error Error
11
12
13
14
15
16
17
18
19
20
PS C:\scripts>

Free Windows Admin Tool Kit Click here and download it now
June 26th, 2015 5:11am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics